package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ChouJiangHistoryListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.ChouJiangRecordListObject;
import com.iexbuy.ihg.entity.ChouJiangRecordListObjectResponce;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeYunGouRecordsFragment extends BaseFragment {

	private static WeDeYunGouRecordsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.returnBtn)
	private Button returnBtn;
	@ViewInject(R.id.selectTypeGroup)
	private RadioGroup selectTypeGroup;

	private int selectType = 0;

	private List<ChouJiangRecordListObject> chouJiangRecordListObjects = new ArrayList<ChouJiangRecordListObject>();
	private int pageNo = 0;
	private ChouJiangHistoryListAdapter chouJiangHistoryListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_yunguo_records, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitleLayoutVisibility(false);
		mBaseActivity.setHomeActivity(false);
		returnBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				mBaseActivity.handleBack();
			}
		});

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});

		selectTypeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int id) {
				String tag = radioGroup.findViewById(id).getTag().toString();
				selectType = Integer.valueOf(tag);
				pageNo = 0;
				initData();
			}
		});

		msgListRefreshLayout.autoRefresh();
	}

	private void loadMore() {
		pageNo ++;
		initData();
	}

	public void initData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		String Url = ServiceHelper.GET_CHOU_JIAN_HISTORY;
		if (selectType == 1) {
			Url = ServiceHelper.GET_CHOU_JIAN_LUCK_HISTORY;
		}
		ServiceHelper.send(mBaseActivity, Url, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ChouJiangRecordListObjectResponce rep = JsonUtils.parseJsonStrToObject(responseInfo.result, ChouJiangRecordListObjectResponce.class);
						if (pageNo == 0) {
							chouJiangRecordListObjects.clear();
						}
						chouJiangRecordListObjects.addAll(rep.getBody());
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						if (pageNo == 0) {
							chouJiangRecordListObjects.clear();
							initListView();
						} else {
							ToastUtils.toast(mBaseActivity, "没有更多记录了");
						}
					}
				});
	}

	private void initListView() {
		if (chouJiangHistoryListAdapter == null) {
			chouJiangHistoryListAdapter = new ChouJiangHistoryListAdapter(mBaseActivity, chouJiangRecordListObjects);
			listView.setAdapter(chouJiangHistoryListAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					WeDeYunGouRecordDetailFragment.setChouJiangRecordListObject(chouJiangRecordListObjects.get(i));
					CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeYunGouRecordDetailFragment.class);
				}
			});
		}
		chouJiangHistoryListAdapter.notifyDataSetChanged();
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeYunGouRecordsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
